package com.example.a2032.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.List;

public class ViewPagerVerticalAdapter extends FragmentStateAdapter {
    int count=1;
    List<Fragment> list;

    public ViewPagerVerticalAdapter(@NonNull FragmentActivity fragmentActivity, List<Fragment> list) {
        super(fragmentActivity);
        this.list = list;
    }

    public ViewPagerVerticalAdapter(@NonNull Fragment fragment,List<Fragment> list, int count ) {
        super(fragment);
        this.count = count;
        this.list = list;
    }

    public void updateData(List<Fragment> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public Fragment getFragment(int position) {
        return list.get(position);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {

        return getFragment(position%getRealCount());
    }

    @Override
    public int getItemCount() {
        return list.size()*count;
    }

    //重写此方法，否则 notifyDataSetChanged()无效
    @Override
    public long getItemId(int position) {
        return list.get(position%getRealCount()).hashCode();
    }

    @Override
    public boolean containsItem(long itemId) {
        for(Fragment fragment:list){
            if(fragment.hashCode()==itemId)return true;
        }

        return false;
    }


    int getRealCount(){
        return list==null?0:list.size();
    }
}
